<!-- 文件模板 -->
<template>
	<div class="t-wp" v-loading="state.loading">
		<el-card shadow="hover" :body-style="{ paddingBottom: '0' }">
			<el-form class="text-right" :model="state.queryParams" ref="queryForm" :inline="true">
				<!-- <el-form-item label="模板名称">
					<el-input v-model="state.queryParams.aaa" placeholder="请输入"/>
				</el-form-item>
				<el-form-item>
					<el-button-group>
						<el-button type="primary" icon="ele-Search" @click="handleQuery"> 查询 </el-button>
						<el-button icon="ele-Refresh" @click="resetQuery"> 重置 </el-button>
					</el-button-group>
				</el-form-item> -->
				<el-form-item>
					<el-button type="primary" icon="ele-Plus" @click="uploadFile()"> 上传 </el-button>
				</el-form-item>
			</el-form>
		</el-card>
		<el-card class="full-table" shadow="hover" style="margin-top: 8px">
			<el-table :data="state.list" style="width: 100%" border>
				<el-table-column type="index" label="序号" width="55" align="center" />
				<el-table-column prop="fileName" label="文件模板名称" show-overflow-tooltip />
				<el-table-column label="操作" width="200" align="center" fixed="right" show-overflow-tooltip>
					<template #default="scope">
						<SeeFile :name="scope.row.fileName" :url="scope.row.url"/>
						<el-button icon="el-icon-delete" size="small" text type="primary" @click="deleteRow(scope.row)">删除文件</el-button>
					</template>
				</el-table-column>
			</el-table>
		</el-card>

		<input v-show="false" ref="fileRef" type="file" @change="fileChange">
	</div>
</template>

<script setup name="SupplierManage">
import { onMounted, reactive, ref } from 'vue';
import {useRouter, useRoute} from "vue-router";
import { ElMessage, ElMessageBox, ElNotification } from 'element-plus';
import {$get, $post, $postparams, $postFile} from '/@/utils/request';
import SeeFile from "/@/components/SeeFile/index.vue";

const router = useRouter();
const route = useRoute();
const fileRef = ref();

const PUBLIC_PATH = import.meta.env.VITE_PUBLIC_PATH;
const state = reactive({
	loading: false,
	queryParams: {
		aaa: '',
		bbb: '',
	},

	list: []
});

onMounted(async () => {
	handleQuery();
});

// 查询操作
const handleQuery = async () => {
	state.loading = true;
	var res = await $get('/projectService/gettemplatefiles/0');
	state.list = res.result.map(item => {
		item.fileName = item.fileName+item.suffix;
		return item;
	});
	state.loading = false;
};

// 删除行
const deleteRow = row => {
	ElMessageBox.confirm('此操作将删除该模板，是否继续？', '提示', {
		type: 'warning'
	}).then(async () => {
		var res = await $post('/projectService/removeformfile/'+row.id);
		ElNotification({
	    title: '提示',
	    message: '删除成功',
	    type: 'success',
	  })
	  // 更新列表
	  handleQuery();
	})
}

// 上传附件
function uploadFile() {
	fileRef.value.click();
}
// 上传附件改变
function fileChange(e) {
	let files = e.target.files;
	if(!files.length) {
		return;
	}
	let file = files[0];
	let fd = new FormData();
  fd.append("file", file);
  $postFile(`/projectService/uploadformfile?path=filetemplate&isTemplate=true`, fd).then(res => {
  	if(res.code == 200) {
  		ElNotification({
		    title: '提示',
		    message: '上传成功',
		    type: 'success',
		  })
		  handleQuery();
  	}
  }).finally(() => {
  	e.target.value = '';
  })
}

</script>

<style lang="scss" scoped>
.t-wp {
	
}
</style>
